package top.suven.base.util;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName:
 * @Description:
 * @Author liulu
 * @Date 2018/5/15 10:53
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 */
public class DateUtils {
    public static String _yyyyMMddHHmmss= "yyyy-MM-dd HH:mm:ss";
    public static String yyyyMMddHHmmss="yyyyMMdd HH:mm:ss";
    public static String _yyyyMMddHHmmssSSS="yyyy-MM-dd HH:mm:ss.SSS";
    public static String yyyyMMddHHmmssNoBlank="yyyyMMddHHmmss";
    public static String yyyyMMdd="yyyyMMdd";
    public static String yyyyMMddHH="yyyyMMddHH";
    public static String yyMMdd="yyMMdd";
    public static String yyyyMM="yyyyMM";
    public static String _yyyyMMdd="yyyy-MM-dd";
    public static String _yyyyMM="yyyy-MM";
    public static String _yyyyMMddHHmm= "yyyy-MM-dd HH:mm";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static String getDate(long millis) {
        if (millis < 0) {
            return null;
        }
        return DateFormatUtils.format(millis, _yyyyMMddHHmm);
    }

    public static String getDateString(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }


    public static Date parseDate(String str) {
        if (StringUtils.isBlank(str)){
            return null;
        }
        try {
            return  org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static double getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
    }

    /**
     * 获取两个时间中的具体多少天，小时，分钟
     * @param fDate
     * @param oDate
     * @return
     */
    public static String getIntervalTimes(Date fDate, Date oDate){
        if (null == fDate || null == oDate) {
            return null;
        }
        long intervalMilli = oDate.getTime() - fDate.getTime();
        if(intervalMilli < 0){
            return null;
        }
        String expireTime = "";
        long d=intervalMilli /  (24 * 60 * 60 * 1000);//天
        long h=(intervalMilli-d*(24 * 60 * 60 * 1000)) / (60 * 60 * 1000);//小时
        long m=(intervalMilli-d*(24 * 60 * 60 * 1000)-h*(60 * 60 * 1000)) / 60000;//分钟
        long s=(intervalMilli-d*(24 * 60 * 60 * 1000)-h*(60 * 60 * 1000) - m*(60 * 1000)) / 1000;//秒
        if(d > 0){
            expireTime = (d > 0 ? d+"天" : "") + (h > 0 ? h+"小时" : "");
        }else if(!(d > 0) && h > 0){
            expireTime = (h > 0 ? h+"小时" : "") + (m > 0 ? m+"分钟" : "");
        }else if(!(d > 0) && !(h > 0) && m > 0){
            expireTime = (m > 0 ? m+"分钟" : "");
        }else if(!(d > 0) && !(h > 0) && !(m > 0) && s > 0){
            expireTime = (s > 0 ? s+"秒" : "");
        }
        return expireTime;
    }




    /**
     * 获取两个日期之间的小时
     *
     * @param before
     * @param after
     * @return
     */
    public static double getDistanceOfTwoDateH(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60);
    }

    /**
     * 获取两个日期之间的分钟
     *
     * @param before
     * @param after
     * @return
     */
    public static double getDistanceOfTwoDateM(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 );
    }

    /**
     * 获取当天周几(1至7分别对应星期一至星期天）
     * @return
     */
    public static int getWeekDay(){
        Date today = new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if(weekday == 1){
            weekday = weekday + 6;
        }else{
            weekday--;
        }
        return weekday;
    }
}
